
package com.monlyu.action.solution;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import com.monlyu.action.BaseAction;
import com.monlyu.bo.ManuscriptBo;
import com.monlyu.bo.SolutionBo;
import com.monlyu.bo.UserBo;
import com.monlyu.exception.MsgException;
import com.monlyu.listener.InitDataListener;
import com.monlyu.model.ShowSolutionModel;
import com.monlyu.util.ActionUtil;
import com.monlyu.util.DateUtil;
import com.monlyu.util.FtlUtil;
import com.monlyu.vo.Solution;
import com.monlyu.vo.SolutionContent;
import com.monlyu.vo.SolutionItem;
import com.monlyu.vo.SolutionReview;
import com.monlyu.vo.User;
import com.sun.org.apache.bcel.internal.generic.NEW;

/*******************************************************************************
 * <b>功能描述☞</b>版面信息编辑<br>
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>news_cd<br>
 * <b>文件路径☞</b>com.monlyu.action.solution.EditSolutionAction.java<br>
 * <b>开发日期☞</b>2011-6-25 <br>
 * 
 * @author chendan
 * @since JDK 5
 * @version 1.0.8
 ******************************************************************************/
public class EditSolutionAction extends BaseAction {

    private String content;
    private Solution solution;
    private SolutionBo solutionBo;
    private List<SolutionContent> contentList;
    private String renderHtml;
    private SolutionReview review;
    private ShowSolutionModel showSolutionModel;
    private int level;
    private String macrIds;
    private ManuscriptBo manuscriptBo;
    private UserBo userBo;

    public void setUserBo(UserBo userBo){
        this.userBo = userBo;
    }

    public void setContent(String content){
        this.content = content;
    }

    public void setMacrIds(String macrIds){
        this.macrIds = macrIds;
    }

    public int getLevel(){
        return level;
    }

    public void setLevel(int level){
        this.level = level;
    }

    public ShowSolutionModel getShowSolutionModel(){
        return showSolutionModel;
    }

    public void setShowSolutionModel(ShowSolutionModel showSolutionModel){
        this.showSolutionModel = showSolutionModel;
    }

    public SolutionReview getReview(){
        return review;
    }

    public void setReview(SolutionReview review){
        this.review = review;
    }

    public List<SolutionContent> getContentList(){
        return contentList;
    }

    public void setContentList(List<SolutionContent> contentList){
        this.contentList = contentList;
    }

    public Solution getSolution(){
        return solution;
    }

    public void setSolution(Solution solution){
        this.solution = solution;
    }

    public void setSolutionBo(SolutionBo solutionBo){
        this.solutionBo = solutionBo;
    }

    /**
     * 保存编辑方案信息
     * 
     * @return
     * @throws UnsupportedEncodingException
     * @since chendan 2010-2-15
     */
    public String execute(){
        if (solution == null) {
            return ajax();
        } else if (solution.getId() == null) {
            // 新建
            solutionBo.createSolution(solution);
        } else if (solution.getId() != null) {
            // 修改
            solutionBo.updateSolution(solution);
        }
        return ajax(solution.getId());
    }

    /**
     * 保存编辑方案内容
     * 
     * @return
     * @since chendan 2011-6-26
     */
    public String editContent(){
        // if (solution == null || solution.getId() == null) {
        // throw new MsgException("请先保存编辑方案");
        // } else {
        // if (content.getId() == null) {
        // // 新建
        // solutionBo.createContent(content);
        // } else if (content.getId() != null) {
        // // 修改
        // // solutionBo.updateSolution(content);
        // }
        // contentList = solutionBo.getContent(content.getSolution().getId());
        // }
        // // return ajax(content.getId());
        return view("editSolutionContent.jsp");
    }

    /**
     * 跳转到编辑方案审核页面
     * 
     * @return
     * @since chendan 2011-6-30
     */
    public String toReview(){
        // 查询编辑方案详细信息及内容
        if (solution != null && solution.getId() != null) {
            showSolutionModel = solutionBo.getShowSolution(solution.getId());
        }
        return view("reviewSolution.jsp");
    }

    /**
     * 编辑方案审核
     * 
     * @return
     * @since chendan 2011-6-30
     */
    public String review(){
        // 查询编辑方案详细信息及内容
        try {
            if (solution != null && solution.getId() != null) {
                review.setSolution(solution);
                solutionBo.review(review);
                showSolutionModel = solutionBo.getShowSolution(solution.getId());
            }
            level = 0;
            // return view("reviewSolution.jsp");
        }
        catch (Exception e) {
            return ajax(e.getMessage());
        }
        return ajax();
    }

    /**
     * 跳转到编辑方案新增、修改页面
     * 
     * @return
     * @since chendan 2011-6-30
     */
    public String toEdit(){
        if (solution != null && solution.getId() != null) {
            solution = solutionBo.get(Solution.class, solution.getId());
        }
        return view("solutionForm.jsp");
    }

    public String toEditContent(){
        if (solution != null && solution.getId() != null) {
            try {
                solutionBo.load(solution);
                render2Html(solution);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
        return view("editSolutionContent.jsp");
    }

    private void render2Html(Solution solution) throws Exception{
        Map<String, Object> title = new HashMap<String, Object>();
        title.put("schemaname", solution.getName());
        title.put("issue", solution.getIssue());
        title.put("pubMonth", DateUtil.format(solution.getDate(), "MM"));
        title.put("pubDay", DateUtil.format(solution.getDate(), "dd"));
        title.put("year", DateUtil.format(solution.getRecorderDate(), "yyyy"));
        title.put("month", DateUtil.format(solution.getRecorderDate(), "MM"));
        title.put("day", DateUtil.format(solution.getRecorderDate(), "dd"));
        Map<String, Object> content = new HashMap<String, Object>();
        content.put("title", FtlUtil.render2Ftl(title, "scheme/title.html"));

        Map<String, Object> thead = new HashMap<String, Object>();
        content.put("thead", FtlUtil.render2Ftl(thead, "scheme/head.html"));

        if (solutionBo.isNewItem(solution.getId())) {
            content.put("tbody", "");
        } else {
            Map<String, Object> tbody = new HashMap<String, Object>();
            tbody.put("isNoew", false);
            tbody.put("contents", solutionBo.getItems(solution.getId(), false));
            content.put("tbody", FtlUtil.render2Ftl(tbody, "scheme/body.html"));
        }
        renderHtml = FtlUtil.render2Ftl(content, "scheme/mainPage.html");
    }

    /*
     * 保存
     */
    public String saveSolution(){
        JSONObject obj = JSONObject.fromObject(content);
        Long solid = obj.getLong("solution.id");
        if (solid == null)
            throw new MsgException("没有对应的ID编号的类型");
        try {
            solutionBo.saveSolution(obj);
            return ajax("OK");
        }
        catch (Exception e) {
            return ajax(e.getMessage());
        }
    }

    // 动态生成
    public String ajaxGrenter(){
        String ids[] = macrIds.split(";");
        Map<String, Object> ftlData = new HashMap<String, Object>();
        ftlData.put("map", manuscriptBo.findScript(ids));
        ftlData.put("isNoew", true);
        try {
            String uers = createUserList(null);
            ftlData.put("users", uers);
            String ftl = FtlUtil.render2Ftl(ftlData, "scheme/body.html");
            return ajax(ftl);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return ajax("Error");
    }

    /**
     * 创建数据列表
     * 
     * @return
     * @throws Exception
     */
    public static String createUserList(User user) throws Exception{
        Map<String, Object> users = new HashMap<String, Object>();
        users.put("users", ((UserBo) InitDataListener.getBeanById("userBo")).findUsers());
        if (user != null && user.getId() != null) {
            users.put("selectId", user.getId());
        }else{
            users.put("selectId", 0l);
        }
        String uers = FtlUtil.render2Ftl(users, "scheme/selectEditor.html");
        return uers;
    }

    /**
     * 生成zip文件
     */
    public String showAndPrint(){
        solutionBo.load(solution);
        decType = ActionUtil.SITEMESH_BODY;
        titleType = ActionUtil.SITEMESH_TITLE_NONE;
        try {
            render2Html(solution);
        }
        catch (Exception e) {
            renderHtml = e.getMessage();
        }
        return view("showAndPrint.jsp");
    }

    public String getRenderHtml(){
        return renderHtml;
    }

    public void setManuscriptBo(ManuscriptBo manuscriptBo){
        this.manuscriptBo = manuscriptBo;
    }
}
